package com.flute.icrawler.policy.bdb.storage;

import java.io.File;

import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

/**
 * Berkeley DB Environment
 * 
 */
public class BdbEnvironment extends Environment {
	private StoredClassCatalog classCatalog;
	private Database classCatalogDB;

	/**
	 * Constructor
	 * 
	 * @param envHome
	 * @param envConfig
	 * @throws DatabaseException
	 */
	public BdbEnvironment(File envHome, EnvironmentConfig envConfig) throws DatabaseException {
		super(envHome, envConfig);
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public StoredClassCatalog getClassCatalog() {
		if (classCatalog == null) {
			DatabaseConfig dbConfig = new DatabaseConfig();
			dbConfig.setAllowCreate(true);
			try {
				classCatalogDB = openDatabase(null, "classCatalog", dbConfig);
				classCatalog = new StoredClassCatalog(classCatalogDB);
			} catch (DatabaseException e) {
				throw new RuntimeException(e);
			}
		}
		return classCatalog;
	}

	/**
	 * 
	 * 
	 */
	public void close() throws DatabaseException {
		if (classCatalogDB != null) {
			classCatalogDB.close();
		}
		super.cleanLog();
		super.close();
	}
}
